﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

using com.vs.General;
using com.vs.DataAccess;
using com.vs.DataAccess.DO;

namespace com.vs.Receiving.BL
{
    public class ReceiptContManager
    {
        public ReceiptContainerDO NewReceiptContainer(Session sess, int iIntRecLnNum)
        {
            ReceiptDetailDO rdDO = null;

            rdDO = ReceiptDetailDO.GetModelByPK(sess, iIntRecLnNum);
            if (null == rdDO)
                //return MessageManager.MSG_RCP_002;
                return null;
            return this.NewReceiptContainer(sess, rdDO);
        }

        public ReceiptContainerDO NewReceiptContainer(Session sess, ReceiptDetailDO rdDO)
        {
            ReceiptContainerDO rcDO = null;

            rcDO = new ReceiptContainerDO();
            rcDO.INTERNAL_RECEIPT_NUM = rdDO.INTERNAL_RECEIPT_NUM;
            rcDO.INTERNAL_RECEIPT_LINE_NUM = rdDO.INTERNAL_RECEIPT_LINE_NUM;
            rcDO.RECEIPT_ID = rdDO.RECEIPT_ID;
            rcDO.WAREHOUSE = rdDO.WAREHOUSE;
            rcDO.COMPANY = rdDO.COMPANY;
            rcDO.ITEM = rdDO.ITEM;
            rcDO.ITEM_DESC = rdDO.ITEM_DESC;
            rcDO.ATTRIBUTE1 = rdDO.ATTRIBUTE1;
            rcDO.ATTRIBUTE2 = rdDO.ATTRIBUTE2;
            rcDO.ATTRIBUTE3 = rdDO.ATTRIBUTE3;
            rcDO.ATTRIBUTE4 = rdDO.ATTRIBUTE4;
            rcDO.ATTRIBUTE5 = rdDO.ATTRIBUTE5;
            rcDO.ATTRIBUTE6 = rdDO.ATTRIBUTE6;
            rcDO.ATTRIBUTE7 = rdDO.ATTRIBUTE7;
            rcDO.ATTRIBUTE8 = rdDO.ATTRIBUTE8;
            rcDO.QUANTITY_UM = rdDO.QUANTITY_UM;
            rcDO.STATUS = (int)Constants.InboundStatus.iLOCATE_PENDING;
            rcDO.LOCATING_RULE = rdDO.LOCATING_RULE;
            rcDO.USER_STAMP = sess.User;

            return rcDO;
        }

        /// <summary>
        /// 返回拆分出来的container号
        /// </summary>
        /// <param name="sess"></param>
        /// <param name="iRecContNum"></param>
        /// <param name="iSplitQty"></param>
        /// <returns></returns>
        public int SplitReceiptContainer(Session sess, int iRecContNum, int iSplitQty)
        {
            DataHelper helper = null;

            try
            {
                helper = new DataHelper(sess);
                return helper.GetInt(CommandType.StoredProcedure, "RCP_SplitReceiptContainer", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_REC_CONT_NUM", iRecContNum),
                    DataHelper.BuildParameter(sess, "@QUANTITY", iSplitQty)});

            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return 0;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public bool IsParentContainer(Session sess, int iRecContNum)
        {            
            DataHelper helper = null;
            string stSQL = "RCP_IsParentCont";
            int iChildren = 0;

            try
            {
	            helper = new DataHelper(sess);
                iChildren = helper.GetInt(CommandType.StoredProcedure, stSQL, new IDataParameter[] { 
				            DataHelper.BuildParameter(sess, "@INTERNAL_CONTAINER_NUM", iRecContNum)
			            });
                return iChildren > 0 ? true : false;
            }
            catch (Exception e)
            {
	            ExceptionManager.LogException(sess, e);
	            return true;
            }
            finally
            {
	            if (helper != null)
		            helper.Dispose();
            }
        }

        /// <summary>
        /// 根据LPN号更新收货LPN状态
        /// </summary>
        /// <param name="sess"></param>
        /// <param name="stContID"></param>
        /// <returns></returns>
        public string UpdateContainerStatus(Session sess, int iRecContNum, int iStatus)
        {
            //DataHelper helper = null;
            //string stSQL = "RCP_UReceiptContSts";
            ReceiptContainerDO rcDO = null;
            Dictionary<string, object> dcValue = null;

            try
            {
                //helper = new DataHelper(sess);
                //helper.Update(CommandType.StoredProcedure, stSQL, new IDataParameter[] { 
                //            DataHelper.BuildParameter(sess, "@INTERNAL_CONTAINER_NUM", iRecContNum),
                //            DataHelper.BuildParameter(sess, "@STATUS", iStatus)
                //    });

                rcDO = new ReceiptContainerDO();
                rcDO.INTERNAL_CONTAINER_NUM = iRecContNum;

                dcValue = new Dictionary<string, object>();
                dcValue.Add("STATUS", iStatus);
                rcDO.Update(sess, dcValue);

                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000;
            }
            finally
            {
                //if (helper != null)
                //    helper.Dispose();
            }
        }

        public string UpdateContainerStatusByWave(Session sess, int iWaveNum, int iStatus)
        {
            DataHelper helper = null;
            string stSQL = "RCP_UReceiptContStsByWave";

            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, new IDataParameter[] { 
				            DataHelper.BuildParameter(sess, "@INTERNAL_WAVE_NUM", iWaveNum),
                            DataHelper.BuildParameter(sess, "@STATUS", iStatus)
			            });
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string SaveUpdateFromUI(Session sess, ReceiptContainerDO rcDO)
        {
            //string stErr = null;
            Dictionary<string, object> dcValue = null;
            LocationDO locDO = null;
            ReceiptContainerDO curDO = null;

            try
            {
                if (rcDO == null)
                    return null;

                curDO = ReceiptContainerDO.GetModelByPK(sess, rcDO.INTERNAL_CONTAINER_NUM);
                if (curDO.STATUS > (int)Constants.InboundStatus.iLOCATE_PENDING)
                    return MessageManager.MSG_RCP_032; //托盘目前状态无法修改

                locDO = LocationDO.GetModelByPK(sess, rcDO.TO_LOCATION, curDO.WAREHOUSE);
                if (locDO == null)
                    return MessageManager.MSG_LOC_001; //库位不存在                

                dcValue = new Dictionary<string, object>();
                dcValue.Add("LOCATING_RULE", rcDO.LOCATING_RULE);
                dcValue.Add("INVENTORY_STS", rcDO.INVENTORY_STS);
                dcValue.Add("TO_LOCATION", rcDO.TO_LOCATION);
                rcDO.Update(sess, dcValue);

                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }
    }
}
